package ivan.cheng.receivers;

import ivan.cheng.Const;
import ivan.cheng.R;
import ivan.cheng.activitys.AppDetailActivity;
import ivan.cheng.utils.LogUtil;
import ivan.cheng.utils.PreferencesUtils;
import ivan.cheng.utils.Util;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class InstallUninstallReceiver extends BroadcastReceiver 
{
	@Override
	public void onReceive(Context context, Intent intent) {
		// 接收广播：设备上新安装了一个应用程序包后自动启动新安装应用程序。
		if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
			String packageName = intent.getDataString().substring(8);
			System.out.println("---------------" + packageName);
			if(false == PreferencesUtils.getBoolValue(context,
					PreferencesUtils.KEY_NOTIFICATION_ON,true))
				return;
			try 
			{
				PackageManager pm = context.getPackageManager();
				PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
				if(null == packageInfo.requestedPermissions)
					return;
				int w = Util.GetWarningPermissionCount(packageInfo,pm);
				StringBuilder sb = new StringBuilder();
				sb.append("\"").append(packageInfo.applicationInfo.loadLabel(pm).toString())
				.append("\"").append(context.getResources().getString(
							R.string.permission_count,
							packageInfo.requestedPermissions.length))
				.append(",")
				.append(context.getResources().getString(
						R.string.dangdrous_count,w))
				.append(",")
				.append(context.getResources().getString(
						R.string.permission_check));
				showNotificationFling(context,packageName.hashCode(),sb.toString(),packageName);
				
			} 
			catch (NameNotFoundException e) 
			{
				e.printStackTrace();
				LogUtil.w(e.toString());
			}
			
		}
		// 接收广播：设备上删除了一个应用程序包。
		if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
			String packageName = intent.getDataString().substring(8);
			System.out.println("********************************");
			// DatabaseHelper dbhelper = new DatabaseHelper();
			// dbhelper.executeSql("delete from users");
		}
	}
	
	private void showNotificationFling(Context context,int notifyID,String textContent,String packageName) 
	{

		String msg = null;
		int icon = R.drawable.ic_launcher;
		// Notification的内容标题，拖下来后看到的标题
		String contentTitle = context.getString(R.string.app_name);
		// Notification的内容
		String contentText = textContent;
		Intent intent = new Intent(context, AppDetailActivity.class);
		intent.putExtra(Const.EXTRA_PACKAGE_NAME, packageName);
		PendingIntent contentIntent = PendingIntent.getActivity(context, notifyID,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		// 创建Notifcation
		Notification notification = new Notification(icon, contentText, System
				.currentTimeMillis());
		// 指定Flag，Notification.FLAG_AUTO_CANCEL意指点击这个Notification后，立刻取消自身
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		// 显示这个notification
		mNotificationManager.notify(notifyID, notification);
	}
}